"""服务托管的信息获取"""

from src.core import Enums
from src.db import dbs, dbm
from src.mod import safe_kit
from src.mod import convert_custom_time
from src.core.variables.function import *
from src.schema.hosting import info_schema
from src.core.variables.constant import CONF
from src.core.variables.status import SUCCESS_CODE_CUSTOMIZE


@safe_kit.mn_node_translate
@safe_kit.all_apps
def all_apps(
    user,
) -> dict:
    """
    获取所有app的信息
    @param origin: 请求来源，来自于用户还是来自于节点
    """
    result = {}
    with dbm.get("hosting_Java") as db_session:
        from sqlalchemy.orm import scoped_session

        db_session: scoped_session
        db_result = db_session.query(dbs.hosting_Java.App).all()
        for i in db_result:
            temp = {}
            for field, value in i.__dict__.items():
                if type(value) is str:
                    temp[field] = value
                if field in ["deploy_time", "time_dir", "create_time", "update_time"]:
                    temp[field] = convert_custom_time(value)
            temp["stype"] = Enums.Hosting.Stypes.JAVA.value
            result[i.app_id] = temp
    with dbm.get("hosting_Static") as db_session:
        db_result = db_session.query(dbs.hosting_Static.App).all()
        for i in db_result:
            temp = {}
            for field, value in i.__dict__.items():
                if type(value) is str:
                    temp[field] = value
                if field in ["deploy_time", "time_dir", "create_time", "update_time"]:
                    temp[field] = convert_custom_time(value)
            result[i.app_id] = temp
            temp["stype"] = Enums.Hosting.Stypes.STATIC.value
    return result, user


def java(schema: info_schema, user):
    """java应用的信息"""
    all_apps_result = all_apps(user)
    now_data = all_apps_result[schema.app_id]
    with dbm.get("hosting_Java") as db_session:
        from sqlalchemy.orm import scoped_session

        db_session: scoped_session
        apps = (
            db_session.query(dbs.hosting_Java.App_backup)
            .filter(dbs.hosting_Java.App_backup.app_id == schema.app_id)
            .all()
        )
        temp_deploy_history = {}
        for app in apps:
            temp = {}
            for field, value in app.__dict__.items():
                if type(value) is str:
                    temp[field] = value
                if field in ["deploy_time", "time_dir", "create_time", "update_time"]:
                    temp[field] = convert_custom_time(value)
            temp_deploy_history[app.back_id] = temp
        # 根据before_id排序
        deploy_history = {}
        n, befor_id = 0, "0"
        while befor_id in [
            item.get("before_id") for item in temp_deploy_history.values()
        ]:
            for key, value in temp_deploy_history.items():
                if str(value.get("before_id", "")) == str(befor_id):
                    deploy_history[n] = value
                    n += 1
                    befor_id = key
                    break
        result = {
            "app_id": schema.app_id,
            "now_data": now_data,
            "deploy_history": deploy_history,
        }
        return SUCCESS_CODE_CUSTOMIZE(result, 200)


def static(schema: info_schema, user):
    """静态应用的信息"""
    all_apps_result = all_apps(user)
    now_data = all_apps_result[schema.app_id]
    with dbm.get("hosting_Static") as db_session:
        deploy_history = {}
        apps = (
            db_session.query(dbs.hosting_Static.App_backup)
            .filter(dbs.hosting_Static.App_backup.app_id == schema.app_id)
            .all()
        )
        for app in apps:
            temp = {}
            for field, value in app.__dict__.items():
                if type(value) is str:
                    temp[field] = value
                if field in ["deploy_time", "time_dir", "create_time", "update_time"]:
                    temp[field] = convert_custom_time(value)
                deploy_history[app.back_id] = temp
        result = {
            "app_id": schema.app_id,
            "now_data": now_data,
            "deploy_history": deploy_history,
        }
        return SUCCESS_CODE_CUSTOMIZE(result, 200)


def fm_nginx_template_conf():
    """获取nginx的模板配置文件"""
    with open(f"{CONF.PATH.FM_NGINX_TEMPLATE_CONF}", "r") as file:
        return "".join(file.readlines())
